package org.zyj.io.idle;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelOption;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;

import java.util.concurrent.ExecutionException;

/**
 */
public class Server {

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        final NioEventLoopGroup bossGroup = new NioEventLoopGroup(1);
        final NioEventLoopGroup workGroup = new NioEventLoopGroup(4);
        try {
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap //
                    .group(bossGroup, workGroup)//
                    .channel(NioServerSocketChannel.class) //
                    .option(ChannelOption.SO_BACKLOG, 128)  //
                    .option(ChannelOption.SO_KEEPALIVE, true) //
                    .handler(new LoggingHandler(LogLevel.INFO)) //



                    .childHandler(new IdleInitializer());


            ChannelFuture channelFuture = serverBootstrap.bind(9998).sync();//
            channelFuture.addListener((ChannelFutureListener) future -> {
                if (future.isSuccess()) {
                    System.out.println("服务器启动: 端口监听成功");
                } else {
                    System.out.println("服务器启动: 端口监听失败");
                }

            });
            channelFuture.channel().closeFuture().sync();
        } finally {
            bossGroup.shutdownGracefully();
        }
    }
}
